<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SpamMessages
 *
 * @author QuangKhoi
 */
class SpamMessages {

    public $messageId;
    public $threadId;
    public $streamId;
    public $author;
    public $source;

    public function __construct() {
        
    }
    
    public static function getKeywords($streamId) {
        $userId = User::getCurrentUserId();
        $db = Database::getInstance();
        $query = "SELECT author, source FROM spammessages WHERE userid = $userId AND streamid = $streamId";
        $result = $db->query($query);
        $query = '';
        if ($result->returnCode == 200)
            if ($result->dataTable->numOfRow > 0) {
                $author = array();
                $source = array();
                foreach ($result->dataTable->rows as $row) {
                    if ($row['author'])
                        $author[] = $row['author'];
                    if ($row['source'])
                        $source[] = $row['source'];
                }
                return json_encode(Array('author' => $author, 'source' => $source));
            }
        return null;
    }
    
    public static function getSKeywords($streamId) {
        $userId = User::getCurrentUserId();
        $db = Database::getInstance();
        $query = "SELECT author, source FROM spammessages WHERE userid = $userId AND streamid = $streamId";
        $result = $db->query($query);
        if ($result->returnCode == 200)
            if ($result->dataTable->numOfRow > 0) {
                $author = array();
                $source = array();
                foreach ($result->dataTable->rows as $row) {
                    if ($row['author'])
                        $author[] = $row['author'];
                    if ($row['source'])
                        $source[] = $row['source'];
                }
                return json_encode(Array('author' => $author, 'source' => $source));
            }
        return null;
    }

    public static function getQuery($streamId) {
        $userId = User::getCurrentUserId();
        $db = Database::getInstance();
        $query = "SELECT author, source FROM spammessages WHERE userid = $userId AND streamid = $streamId";
        $result = $db->query($query);
        $query = '';
        if ($result->returnCode == 200)
            if ($result->dataTable->numOfRow > 0) {
                $author = array();
                $source = array();
                foreach ($result->dataTable->rows as $row) {
                    if ($row['author'])
                        $author[] = ' "' . $row['author'] . '" ';
                    if ($row['source'])
                        $source[] = ' "' . $row['source'] . '" ';
                }
                if (count($author))
                    $query = '& ( !@author (' . implode('&', array_unique($author)) . ') ) ';
                if (count($source))
                    $query .= '& ( !@source (' . implode('&', array_unique($source)) . ') )';
                return $query;
            }
        return null;
    }

    public static function getSQuery($streamId) {
        $userId = User::getCurrentUserId();
        $db = Database::getInstance();
        $query = "SELECT author, source FROM spammessages WHERE userid = $userId AND streamid = $streamId";
        $result = $db->query($query);
        $query = '';
        if ($result->returnCode == 200)
            if ($result->dataTable->numOfRow > 0) {
                $author = array();
                $source = array();
                foreach ($result->dataTable->rows as $row) {
                    if ($row['author'])
                        $author[] = ' "' . $row['author'] . '" ';
                    if ($row['source'])
                        $source[] = ' "' . $row['source'] . '" ';
                }
                if (count($author))
                    $query = '& ( @author (' . implode('|', array_unique($author)) . ') ) ';
                if (count($source))
                    $query .= '& ( @source (' . implode('|', array_unique($source)) . ') )';
                return $query;
            }
        return null;
    }

    public static function getByMessageId($messageId, $streamId, $source = '', $author = '') {
        $userId = User::getCurrentUserId();
        $db = Database::getInstance();
        $query = "SELECT * FROM spammessages WHERE userid = $userId AND messageid = $messageId AND streamid = $streamId";
        if ($source)
            $query .= " AND source = '$source'";
        if ($author)
            $query .= " AND author = '$author'";
        $result = $db->query($query);
        $instance = new self();
        if ($result->returnCode == 200)
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                $instance->messageId = $row['messageid'];
                $instance->threadId = $row['threadid'];
                $instance->streamId = $row['streamid'];
                $instance->author = $row['author'];
                $instance->source = $row['source'];
                return $instance;
            }
        return null;
    }

}

?>